local skel = fk.CreateSkill {
  name = "lb_zero__huncai",
  max_branches_use_time = {
    ["drawcard"] = {
      [Player.HistoryPhase] = 1
    },
    ["discard"] = {
      [Player.HistoryPhase] = 1
    },
  },
}

local DIY = require "packages.diy_utility.diy_utility"

Fk:loadTranslationTable {
  ["lb_zero__huncai"] = "魂裁",
  [":lb_zero__huncai"] = "出牌阶段各限一次，你可将手牌向上或向下调整至与本回合弃牌堆中花色数相同，然后对所有手牌数为X的角色造成1点冰冻伤害，此伤害于你本轮下次不因此造成或受到伤害后结算（X为你因此获得或失去的牌数）。",

  ["@[player_string]lb_zero__huncai-round"] = "魂裁",

  ["#lb_zero__huncai"] = "魂裁：你可将手牌向上或向下调整至与本回合弃牌堆中花色数相同",
  ["#lb_zero__huncai_draw"] = "魂裁：你可以摸%arg张牌，然后对所有手牌数为%arg的角色造成1点冰冻伤害，此伤害于你本轮下次不因此造成或受到伤害后结算",
  ["#lb_zero__huncai_dis"] = "魂裁：你可以弃置%arg张手牌，然后对所有手牌数为%arg的角色造成1点冰冻伤害，此伤害于你本轮下次不因此造成或受到伤害后结算",

  ["$lb_zero__huncai1"] = "于阴影中雕刻黎明。",
  ["$lb_zero__huncai2"] = "丧钟，为我而鸣！",
  ["$lb_zero__huncai3"] = "臣服于深渊！",
  ["$lb_zero__huncai4"] = "准备好向我下跪了吗？",
  ["$lb_zero__huncai5"] = "在秩序与疯狂的交界起舞！",
}

local getCenterSuitsNum = function(room)
  local suits = {}
  for _, id in ipairs(DIY.getCenterCards(room)) do
    table.insertIfNeed(suits, Fk:getCardById(id).suit)
  end
  return #suits
end

skel:addEffect("active", {
  mute = true,
  prompt = function(self, player, selected_cards, selected_targets)
    local num = getCenterSuitsNum(Fk:currentRoom()) - player:getHandcardNum()
    if num > 0 then
      return "#lb_zero__huncai_draw:::" .. num
    elseif num < 0 then
      num = math.abs(num)
      return "#lb_zero__huncai_dis:::" .. num
    end
    return "#lb_zero__huncai"
  end,
  can_use = function(self, player)
    local num = getCenterSuitsNum(Fk:currentRoom()) - player:getHandcardNum()
    if num == 0 then return false end
    if num > 0 then
      return skel:withinBranchTimesLimit(player, "drawcard", Player.HistoryPhase)
    else
      return skel:withinBranchTimesLimit(player, "discard", Player.HistoryPhase)
    end
  end,
  target_num = 0,
  card_filter = function(self, player, to_select, selected, selected_targets)
    local num = getCenterSuitsNum(Fk:currentRoom()) - player:getHandcardNum()
    return num < 0 and #selected < math.abs(num)
        and not player:prohibitDiscard(to_select) and table.contains(player:getCardIds("h"), to_select)
  end,
  feasible = function(self, player, selected, selected_cards, card)
    local num = getCenterSuitsNum(Fk:currentRoom()) - player:getHandcardNum()
    return num > 0 or (num < 0 and #selected_cards == math.abs(num))
  end,
  on_use = function(self, room, skillUseEvent)
    local player = skillUseEvent.from
    local cards = skillUseEvent.cards
    local num = math.abs(getCenterSuitsNum(Fk:currentRoom()) - player:getHandcardNum())
    player:broadcastSkillInvoke(skel.name, math.random(1, 2))
    if cards and #cards > 0 then
      room:notifySkillInvoked(player, skel.name, "negative")
      player:addSkillBranchUseHistory(skel.name, "discard")
      room:throwCard(cards, skel.name, player)
    else
      room:notifySkillInvoked(player, skel.name, "drawcard")
      player:addSkillBranchUseHistory(skel.name, "drawcard")
      player:drawCards(num, skel.name)
    end
    if player.dead then return end
    local list = player:getTableMark("@[player_string]lb_zero__huncai-round")
    for _, to in ipairs(room.alive_players) do
      if to:getHandcardNum() == num then
        list[tostring(to.id)] = (list[tostring(to.id)] or 0) + 1
      end
    end
    room:setPlayerMark(player, "@[player_string]lb_zero__huncai-round", list)
  end,
})

local huncai_spec = {
  audio_index = {3, 4, 5},
  anim_type = "offensive",
  is_delay_effect = true,
  can_trigger = function(self, event, target, player, data)
    return player:hasSkill(skel.name) and target == player
  end,
  on_trigger = function(self, event, target, player, data)
    local room = player.room
    local list = {}
    for id, value in pairs(player:getTableMark("@[player_string]lb_zero__huncai-round")) do
      local to = room:getPlayerById(tonumber(id))
      if not to.dead then
        table.insert(list, { to, value })
      end
    end
    room:setPlayerMark(player, "@[player_string]lb_zero__huncai-round", 0)
    if #list > 0 then
      local tos, num = {}, {}
      for _, to in ipairs(room:getAlivePlayers()) do
        for _, value in ipairs(list) do
          if to == value[1] then
            table.insert(tos, value[1])
            table.insert(num, value[2])
            break
          end
        end
      end
      event:setCostData(self, { tos = tos, num = num })
      room:useSkill(player, self, function()
        self:use(event, target, player, data)
      end, event.skill_data)
    end
  end,
  on_use = function(self, event, target, player, data)
    local room = player.room
    local tos = event:getCostData(self).tos
    local num = event:getCostData(self).num
    for i, to in ipairs(tos) do
      if to:isAlive() and num[i] > 0 then
        room:damage{
          from = player,
          to = to,
          damage = num[i],
          damageType = fk.IceDamage,
          skillName = skel.name
        }
      end
    end
  end,
}
skel:addEffect(fk.Damage, huncai_spec)
skel:addEffect(fk.Damaged, huncai_spec)

skel:addAcquireEffect(function(self, player, is_start)
  player.room:addSkill("#CenterArea")
end)

skel:addLoseEffect(function(self, player, is_death)
  player.room:setPlayerMark(player, "@lb_zero__huncai", 0)
end)

return skel
